package Avocado.visual {

	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.VideoLoader;
	import com.greensock.loading.data.VideoLoaderVars;
	import com.greensock.plugins.EndVectorPlugin;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.AsyncErrorEvent;
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	
	public class AvocadoVideoPlayer extends MovieClip {
		
		private var background:Sprite;
		
		private var videoURL:String;
		public var video:VideoLoader;
		private var loaderVars:VideoLoaderVars;
		
		public function AvocadoVideoPlayer(_videoURL:String, _loaderVars:VideoLoaderVars=null) {
			super();
			this.videoURL = _videoURL;
			
			if (_loaderVars) {
				this.loaderVars = _loaderVars;	
			} else {
				this.loaderVars = new VideoLoaderVars;
				this.loaderVars.name('theLoader');
				this.loaderVars.container(this);
				this.loaderVars.width(720);
				this.loaderVars.height(480);
				this.loaderVars.scaleMode('proportionalInside');
				this.loaderVars.bgColor(0x000000);
				this.loaderVars.autoPlay(false);
				this.loaderVars.volume(0);
			}

			this.addEventListener(Event.ADDED_TO_STAGE, initThis);
		}
		
		private function initThis(e:Event):void{
			this.removeEventListener(Event.ADDED_TO_STAGE, initThis);

			setupBackground();
			setupVideo();
		}
		
		public function playVideo():void{
			video.playVideo();
		}
		
		public function cueVideo():void{
			video.playVideo();
			video.pause();
		}

		
		
		private function setupBackground():void{
			trace(this.loaderVars);
			background = new Sprite;
			addChild(background);
		}
		
		private function setupVideo():void {
			video = new VideoLoader(this.videoURL, this.loaderVars);
			video.addEventListener(LoaderEvent.COMPLETE, videoComplete);
			video.load(true);
			
			// addChild(videoLoader);
		}
			private function videoComplete(e:Event):void{
				this.dispatchEvent(new Event(LoaderEvent.COMPLETE));
			}
		



	}
}